home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / reader_requests / wild / support / wabl / dxf2wabl / dxf2wabl.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  15KB  |  492 lines

  1. /*
  2. **    Dxf2WABL: Converts a DXF (with 3DFace statements) into a WABL file.
  3. **          Multiple colors give multiple sectors of the same alien.
  4. **          No relative origin calc: do it in editWABLAlien.
  5. */
  6.  
  7. #include <wabl.h>
  8. #include <exec/exec.h>
  9. #include <exec/libraries.h>
  10. #include <dos/dos.h>
  11. #include <utility/tagitem.h>
  12. #include <utility/hooks.h>
  13. #include <inline/dos.h>
  14. #include <inline/exec.h>
  15. #include <exec/lists.h>
  16.  
  17. extern struct ExecBase *SysBase;
  18. extern struct Library *DOSBase;
  19.  
  20. #define ARG_DXFINPUT    0
  21. #define    ARG_WABLOUTPUT    1
  22. #define    ARG_NOCOLORS    2
  23. #define ARG_QUIET    3
  24. #define ARG_VERBOSE    4
  25.  
  26. #define DOING_WAITFACE    50
  27. #define DOING_FNDFACE    51
  28. #define    DOING_VALUEXA    0
  29. #define    DOING_VALUEXB    1
  30. #define    DOING_VALUEXC    2
  31. #define    DOING_VALUEXD    3
  32. #define    DOING_VALUEYA    4
  33. #define    DOING_VALUEYB    5
  34. #define    DOING_VALUEYC    6
  35. #define    DOING_VALUEYD    7
  36. #define    DOING_VALUEZA    8
  37. #define    DOING_VALUEZB    9
  38. #define    DOING_VALUEZC    10
  39. #define    DOING_VALUEZD    11
  40. #define DOING_VALUECOLOR 12
  41. #define DOING_VALUENULL    99
  42.  
  43. struct DXFGlobal
  44. {
  45.  struct MinList dxg_objs;
  46. };
  47.  
  48. struct DXFObj
  49. {
  50.  struct MinNode    dxo_node;
  51.  struct MinList dxo_faces;
  52.  struct MinList dxo_edges;
  53.  struct MinList dxo_points;
  54.  UWORD        dxo_color;
  55. };
  56.  
  57. struct INTEREdge
  58. {
  59.  struct MinNode ie_node;
  60.  struct DXFPoint *ie_pa;
  61.  struct DXFPoint *ie_pb; 
  62.  UWORD        ie_id;
  63. };
  64.  
  65. struct INTERFace
  66. {
  67.  struct MinNode    if_node;
  68.  struct DXFPoint *if_pa;
  69.  struct DXFPoint *if_pb;
  70.  struct DXFPoint *if_pc;
  71.  struct INTEREdge *if_ea;
  72.  struct INTEREdge *if_eb;
  73.  struct INTEREdge *if_ec;
  74.  UWORD        if_id; 
  75. };
  76.  
  77. struct DXFPoint
  78. {
  79.  struct MinNode dxp_node;
  80.  int        dxp_x;
  81.  int        dxp_y;
  82.  int        dxp_z;
  83.  UWORD        dxp_id;
  84. };
  85.  
  86. static const char Intro[]={"\nDxf2Wabl 1.7\n\0"};
  87. static const char Loading[]={"\nLoadind DXF file.\0"};
  88. static const char NewObj[]={"\nFound new object (color).\0"};
  89. static const char Converting[]={"\nConverting formats.\0"};
  90. static const char CreatePnts[]={"\nCreating points.\0"};
  91. static const char CreateFaces[]={"\nCreating faces.\0"};
  92. static const char CreateEdges[]={"\nCreating edges.\0"};
  93. static const char Saving[]={"\nSaving WABL file.\0"};
  94. static const char Bye[]={"\nEnjoy WILD using !!\n\n\0"};
  95. static const char FndFace[]={"\nFac \0"};
  96. static const char TDFACE[]={"3DFACE\n\0"};
  97. static const char FndXA[]={"XA:\0"};
  98. static const char FndXB[]={"XB:\0"};
  99. static const char FndXC[]={"XC:\0"};
  100. static const char FndXD[]={"XD:\0"};
  101. static const char FndYA[]={"YA:\0"};
  102. static const char FndYB[]={"YB:\0"};
  103. static const char FndYC[]={"YC:\0"};
  104. static const char FndYD[]={"YD:\0"};
  105. static const char FndZA[]={"ZA:\0"};
  106. static const char FndZB[]={"ZB:\0"};
  107. static const char FndZC[]={"ZC:\0"};
  108. static const char FndZD[]={"ZD:\0"};
  109. static const char FndCL[]={"C:\0"};
  110. static const char NumFmt[]={'%','l','d',9,0};
  111. static const char AlienType[]={"Alien\0"};
  112. static const char AlienName[]={"Unnamed\0"};
  113. static const char SectorType[]={"Sector\0"};
  114.              char SectorName[7]={'S','e','c','X','X','X',0};
  115. static const char ShellType[]={"Shell\0"};
  116.              char ShellName[7]={'S','h','e','X','X','X',0};
  117. static const char WireType[]={"Wire\0"};
  118.              char WireName[7]={'W','i','r','X','X','X',0};
  119. static const char NebulaType[]={"Nebula\0"};
  120.              char NebulaName[7]={'N','e','b','X','X','X',0};
  121. static const char FaceType[]={"Face\0"};
  122.              char FaceName[8]={'F','a','c','X','X','X','X',0};
  123. static const char EdgeType[]={"Edge\0"};
  124.              char EdgeName[8]={'E','d','g','X','X','X','X',0};
  125. static const char PointType[]={"Point\0"};
  126.              char PointName[8]={'D','o','t','X','X','X','X',0};
  127. static const char PointX[]={"X\0"};
  128. static const char PointY[]={"Y\0"};
  129. static const char PointZ[]={"Z\0"};
  130. static const char FacePA[]={"PA\0"};
  131. static const char FacePB[]={"PB\0"};
  132. static const char FacePC[]={"PC\0"};
  133. static const char FaceEA[]={"EA\0"};
  134. static const char FaceEB[]={"EB\0"};
  135. static const char FaceEC[]={"EC\0"};
  136. #define EdgePA FacePA
  137. #define EdgePB FacePB
  138.  
  139. ULONG     *outfh=NULL,arg[6];
  140. struct WABL *wabl=NULL;
  141.  
  142. void SayV(char *str)
  143. {
  144.  if (arg[ARG_VERBOSE])
  145.   {
  146.    FPuts(outfh,str);
  147.   }
  148. }
  149.  
  150. void Say(char *str)
  151. {
  152.  if (arg[ARG_QUIET]==0)
  153.   {
  154.    FPuts(outfh,str);
  155.   }
  156. }
  157.  
  158. ULONG WriteHook( register struct Hook* hook __asm("a0"), register ULONG object __asm("a2"), register ULONG message __asm("a1"))
  159. {
  160.  return(FPutC(object,message));
  161. }
  162.  
  163. ULONG RawDoFmtOut( register ULONG car __asm("d0"), register ULONG output __asm("a3") )
  164. {
  165.  FPutC(output,car);
  166. }
  167.  
  168. struct DXFObj *AddDXFObj(struct DXFGlobal *dxf,UWORD col)
  169. {
  170.  struct DXFObj *cob,*nob;
  171.  cob=dxf->dxg_objs.mlh_Head;
  172.  while (nob=cob->dxo_node.mln_Succ)
  173.   {
  174.    if (cob->dxo_color==col) return(cob);
  175.    cob=nob;
  176.   }
  177.  cob=AllocPooled(wabl->WABL_Pool,sizeof(struct DXFObj));
  178.  cob->dxo_color=col;
  179.  Say(NewObj);
  180.  NewList(&cob->dxo_faces);
  181.  NewList(&cob->dxo_edges);
  182.  NewList(&cob->dxo_points);
  183.  AddTail(&dxf->dxg_objs,&cob->dxo_node);
  184.  return(cob);
  185. }
  186.  
  187. void Val(int num,char *str)
  188. {
  189.  int a;
  190.  if ((a=LongToStr(num,str,-1))>0)
  191.   {
  192.    str[a]=0;
  193.   }
  194.  else
  195.   {
  196.    str[0]=0;
  197.   }
  198. }
  199.  
  200. struct INTERFace *AddINTERFace(struct DXFObj *obj,struct DXFPoint *pa,struct DXFPoint *pb,struct DXFPoint *pc,struct INTEREdge *ea,struct INTEREdge *eb,struct INTEREdge *ec)
  201. {
  202.  if ((pa==pb) | (pa==pc) | (pb==pc)) return(0L);
  203.  {
  204.   struct INTERFace *cif,*nif;
  205.   cif=obj->dxo_faces.mlh_Head;
  206.   while (nif=cif->if_node.mln_Succ)
  207.    {
  208.     if (((cif->if_pa==pa) & (cif->if_pb==pb) & (cif->if_pc==pc))|
  209.         ((cif->if_pa==pa) & (cif->if_pb==pc) & (cif->if_pc==pb))|
  210.         ((cif->if_pa==pb) & (cif->if_pb==pa) & (cif->if_pc==pc))|
  211.         ((cif->if_pa==pb) & (cif->if_pb==pc) & (cif->if_pc==pa))|
  212.         ((cif->if_pa==pc) & (cif->if_pb==pa) & (cif->if_pc==pb))|
  213.         ((cif->if_pa==pc) & (cif->if_pb==pb) & (cif->if_pc==pa))) return(0L);
  214.     cif=nif;
  215.    }
  216.   cif=AllocPooled(wabl->WABL_Pool,sizeof(struct INTERFace));
  217.   cif->if_pa=pa;
  218.   cif->if_pb=pb;
  219.   cif->if_pc=pc;
  220.   cif->if_ea=ea;
  221.   cif->if_eb=eb;
  222.   cif->if_ec=ec;
  223.   AddTail(&obj->dxo_faces,&cif->if_node);
  224.   return (cif);
  225.  }
  226. }
  227.  
  228. struct INTEREdge *AddINTEREdge(struct DXFObj *obj,struct DXFPoint *pa,struct DXFPoint *pb)
  229. {
  230.  struct INTEREdge *cie,*nie;
  231.  if (pa==pb) return(0L);
  232.  cie=obj->dxo_edges.mlh_Head;
  233.  while (nie=cie->ie_node.mln_Succ)
  234.   {
  235.    if (((cie->ie_pa==pa) & (cie->ie_pb==pb))|
  236.        ((cie->ie_pa==pb) & (cie->ie_pb==pa))) return(cie);
  237.    cie=nie;
  238.   }
  239.  cie=AllocPooled(wabl->WABL_Pool,sizeof(struct INTEREdge));
  240.  cie->ie_pa=pa;
  241.  cie->ie_pb=pb;
  242.  cie->ie_id=NULL;
  243.  AddTail(&obj->dxo_edges,&cie->ie_node);
  244.  return (cie);
  245. }
  246.  
  247. struct DXFPoint *AddDXFPoint(struct DXFObj *obj,int x,int y,int z)
  248. {
  249.  struct DXFPoint *cpo,*npo;
  250.  cpo=obj->dxo_points.mlh_Head;
  251.  while (npo=cpo->dxp_node.mln_Succ)
  252.   {
  253.    if ((cpo->dxp_x==x) & (cpo->dxp_y==y) & (cpo->dxp_z==z)) return(cpo);
  254.    cpo=npo;
  255.   }
  256.  cpo=AllocPooled(wabl->WABL_Pool,sizeof(struct DXFPoint));
  257.  cpo->dxp_x=x;
  258.  cpo->dxp_y=y;
  259.  cpo->dxp_z=z;
  260.  AddTail(&obj->dxo_points,&cpo->dxp_node);
  261.  return(cpo);
  262. }
  263.  
  264. int main()
  265. {
  266.  ULONG *rda;
  267.  outfh=Output();
  268.  Say(Intro);
  269.  if (rda=ReadArgs("DXFIN/A,WABLOUT/A,NOCOLORS/S,QUIET/S,VERBOSE/S",&arg,0))
  270.   {
  271.    ULONG *fh;
  272.    if (fh=Open(arg[ARG_DXFINPUT],MODE_OLDFILE))
  273.     {
  274.      struct Hook puthook;
  275.      if (wabl=NewWABL())
  276.       {
  277.        char *use;
  278.        struct DXFGlobal dxf;
  279.        NewList(&dxf.dxg_objs);
  280.        Say(Loading);
  281.        if (use=AllocPooled(wabl->WABL_Pool,16384))
  282.         {
  283.          int   xa,xb,xc,xd,ya,yb,yc,yd,za,zb,zc,zd;
  284.          UWORD col;
  285.          UBYTE doing=DOING_WAITFACE;
  286.          while (FGets(fh,use,16384))
  287.           {
  288.        if (doing==DOING_WAITFACE)
  289.         {
  290.          if (StrCmp(use,&TDFACE))
  291.               {
  292.                doing=DOING_FNDFACE;
  293.                SayV(FndFace);
  294.            col=1;
  295.           }
  296.             }
  297.        else
  298.         {
  299.          if (doing==DOING_FNDFACE)
  300.           {ULONG num;
  301.            if ((StrToLong(use,&num))>0) 
  302.             {
  303.              switch (num)
  304.               {
  305.                case 10: {doing=DOING_VALUEXA;SayV(FndXA);break;}
  306.                case 11: {doing=DOING_VALUEXB;SayV(FndXB);break;}
  307.                case 12: {doing=DOING_VALUEXC;SayV(FndXC);break;}
  308.                case 13: {doing=DOING_VALUEXD;SayV(FndXD);break;}
  309.                case 20: {doing=DOING_VALUEYA;SayV(FndYA);break;}
  310.                case 21: {doing=DOING_VALUEYB;SayV(FndYB);break;}
  311.                case 22: {doing=DOING_VALUEYC;SayV(FndYC);break;}
  312.                case 23: {doing=DOING_VALUEYD;SayV(FndYD);break;}
  313.                case 30: {doing=DOING_VALUEZA;SayV(FndZA);break;}
  314.                case 31: {doing=DOING_VALUEZB;SayV(FndZB);break;}
  315.                case 32: {doing=DOING_VALUEZC;SayV(FndZC);break;}
  316.                case 33: {doing=DOING_VALUEZD;SayV(FndZD);break;}
  317.                case 62: {doing=DOING_VALUECOLOR;SayV(FndCL);break;}
  318.                case 0:  {
  319.                          struct DXFObj *obj;
  320.                          struct DXFPoint *pa,*pb,*pc,*pd;
  321.                          struct INTEREdge *eab,*ebc,*eca,*edc,*ead;
  322.                          struct INTERFace *faca,*facb;
  323.                          obj=AddDXFObj(&dxf,col);
  324.                          pa=AddDXFPoint(obj,xa,ya,za);
  325.                          pb=AddDXFPoint(obj,xb,yb,zb);
  326.                          pc=AddDXFPoint(obj,xc,yc,zc);
  327.                          pd=AddDXFPoint(obj,xd,yd,zd);
  328.                          eab=AddINTEREdge(obj,pa,pb);
  329.                          ebc=AddINTEREdge(obj,pb,pc);
  330.                          eca=AddINTEREdge(obj,pc,pa);
  331.                          edc=AddINTEREdge(obj,pd,pc);
  332.                          ead=AddINTEREdge(obj,pa,pd);                         
  333.                          faca=AddINTERFace(obj,pa,pb,pc,eab,ebc,eca);
  334.                          facb=AddINTERFace(obj,pa,pc,pd,eca,ead,edc);                                        
  335.                          doing=DOING_WAITFACE;
  336.                          break;
  337.                         }
  338.                default: {doing=DOING_VALUENULL;break;}
  339.               }
  340.             }
  341.               } // DOINGFOUNDFACE
  342.              else
  343.               {ULONG num;
  344.                if ((StrToLong(use,&num))>0)
  345.                 {
  346.          if ((doing!=DOING_VALUENULL) & (arg[ARG_VERBOSE]!=0)) RawDoFmt(NumFmt,&num,&RawDoFmtOut,outfh);
  347.          switch (doing)
  348.           {
  349.            case DOING_VALUEXA: {xa=num;break;}
  350.            case DOING_VALUEYA: {ya=num;break;}
  351.            case DOING_VALUEZA: {za=num;break;}
  352.            case DOING_VALUEXB: {xb=num;break;}
  353.            case DOING_VALUEYB: {yb=num;break;}
  354.            case DOING_VALUEZB: {zb=num;break;}
  355.            case DOING_VALUEXC: {xc=num;break;}
  356.            case DOING_VALUEYC: {yc=num;break;}
  357.            case DOING_VALUEZC: {zc=num;break;}
  358.            case DOING_VALUEXD: {xd=num;break;}
  359.            case DOING_VALUEYD: {yd=num;break;}
  360.            case DOING_VALUEZD: {zd=num;break;}
  361.            case DOING_VALUECOLOR: {if (arg[ARG_NOCOLORS]==0) col=num;}
  362.           }
  363.                 }
  364.                doing=DOING_FNDFACE;
  365.               } // VALUEHAVE
  366.         }
  367.           } // INPUT PART
  368.           Close(fh);
  369.           
  370.           {
  371.            struct WABLObject *alien,*sector,*shell,*wire,*nebula,*point,*face,*edge;
  372.            ULONG idsector=NULL,idshell=NULL,idwire=NULL,idnebula=NULL,
  373.                 idpoint=NULL,idedge=NULL,idface=NULL;
  374.            Say(Converting);
  375.            if (alien=HaveWABLObject(wabl,&AlienType,&AlienName,wabl))
  376.             {
  377.              struct DXFObj *cob,*nob;
  378.              cob=dxf.dxg_objs.mlh_Head;
  379.              
  380.              while (nob=cob->dxo_node.mln_Succ)
  381.               {
  382.                idsector++;LongToHexStr(idsector,&SectorName[3],3);
  383.                if (sector=HaveWABLObject(wabl,&SectorType,&SectorName,alien))
  384.                 {
  385.                  struct DXFPoint *cpo,*npo;                                          
  386.          idnebula++;LongToHexStr(idnebula,&NebulaName[3],3);
  387.          if (nebula=HaveWABLObject(wabl,&NebulaType,&NebulaName,sector))
  388.           {
  389.            Say(CreatePnts);
  390.            cpo=cob->dxo_points.mlh_Head;
  391.            while (npo=cpo->dxp_node.mln_Succ)
  392.             {
  393.              idpoint++;cpo->dxp_id=idpoint;
  394.             
  395. #define    NamePoint(pnt) LongToHexStr(pnt->dxp_id,&PointName[3],4); 
  396.             
  397.              NamePoint(cpo);
  398.              if (point=HaveWABLObject(wabl,&PointType,&PointName,nebula))
  399.               {
  400.                Val(cpo->dxp_x,use);
  401.                  SetWABLAttr(wabl,&PointX,use,point);
  402.                Val(cpo->dxp_y,use);
  403.                  SetWABLAttr(wabl,&PointY,use,point);
  404.                Val(cpo->dxp_z,use);
  405.                  SetWABLAttr(wabl,&PointZ,use,point);
  406.                 } // POINT
  407.              cpo=npo;
  408.             } 
  409.           } // NEBULA
  410.  
  411.                  idwire++;LongToHexStr(idwire,&WireName[3],3);
  412.          if (wire=HaveWABLObject(wabl,&WireType,&WireName,sector))
  413.           {
  414.            struct INTEREdge *cie,*nie;
  415.            Say(CreateEdges);
  416.            cie=cob->dxo_edges.mlh_Head;
  417.            while (nie=cie->ie_node.mln_Succ)
  418.             {
  419.              idedge++;cie->ie_id=idedge;
  420.  
  421. #define    NameEdge(edg) LongToHexStr(edg->ie_id,&EdgeName[3],4); 
  422.  
  423.              NameEdge(cie);
  424.              if (edge=HaveWABLObject(wabl,&EdgeType,&EdgeName,wire))
  425.               {
  426.                NamePoint(cie->ie_pa);
  427.                  SetWABLFriend(wabl,&EdgePA,&PointType,&PointName,edge);
  428.                NamePoint(cie->ie_pb);
  429.                  SetWABLFriend(wabl,&EdgePB,&PointType,&PointName,edge);
  430.               } // SAVEEDGE
  431.              cie=nie;
  432.             }
  433.           } // WIRE
  434.  
  435.          idshell++;LongToHexStr(idshell,&ShellName[3],3);
  436.          if (shell=HaveWABLObject(wabl,&ShellType,&ShellName,sector))
  437.           {
  438.            struct INTERFace *cif,*nif;
  439.            Say(CreateFaces);
  440.            cif=cob->dxo_faces.mlh_Head;
  441.            while (nif=cif->if_node.mln_Succ)
  442.             {
  443.              idface++;cif->if_id=idface;
  444.              
  445. #define    NameFace(fac) LongToHexStr(fac->if_id,&FaceName[3],4); 
  446.              
  447.              NameFace(cif);
  448.              if (face=HaveWABLObject(wabl,&FaceType,&FaceName,shell))
  449.               {
  450.                NamePoint(cif->if_pa);
  451.                  SetWABLFriend(wabl,&FacePA,&PointType,&PointName,face);
  452.                NamePoint(cif->if_pb);
  453.                  SetWABLFriend(wabl,&FacePB,&PointType,&PointName,face);
  454.                NamePoint(cif->if_pc);
  455.                  SetWABLFriend(wabl,&FacePC,&PointType,&PointName,face);
  456.                NameEdge(cif->if_ea);
  457.                  SetWABLFriend(wabl,&FaceEA,&EdgeType,&EdgeName,face);
  458.                NameEdge(cif->if_eb);
  459.                  SetWABLFriend(wabl,&FaceEB,&EdgeType,&EdgeName,face);
  460.                NameEdge(cif->if_ec);
  461.                  SetWABLFriend(wabl,&FaceEC,&EdgeType,&EdgeName,face);
  462.               } // SAVEFACE
  463.              cif=nif;
  464.             } // SAVEFACES
  465.            
  466.           } // SHELL
  467.                 } // SECTOR
  468.                cob=nob;
  469.               } // SECTORS 
  470.             } // ALIEN 
  471.       } // ELABORATION PART        
  472.  
  473.       {
  474.        if (fh=Open(arg[ARG_WABLOUTPUT],MODE_NEWFILE))
  475.         {
  476.          struct Hook puthook;
  477.              struct TagItem savetags[3]={{WABL_FileHandle,fh},{WABL_PutCharHook,&puthook},{0,0}};
  478.          Say(Saving);
  479.              puthook.h_Entry=&WriteHook;
  480.              SaveWABL(wabl,&savetags[0]);             
  481.              Close(fh);
  482.              Say(Bye);
  483.         } // OUTHAD 
  484.       } // SAVEPART
  485.         } // USE
  486.        FreeWABL(wabl);
  487.       } // WABL
  488.     } // DXFOPEN
  489.   } // READARGS
  490. }
  491.  
  492.